package com.zfx.aop;
import com.zfx.service.MyAopService;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
public class CgLibUtils {

	public static Object cgLib(){

		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(MyAopService.class);
		enhancer.setUseFactory(false);
		enhancer.setCallback(new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				System.out.println("CgLibUtils. --- cgLib().......... ");
				Object o1 = methodProxy.invokeSuper(o, objects);
				return o1;
			}
		});
		Object o = enhancer.create();
		return o;
	}

}
